package com.lucker.service.checkcode;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lucker.cds.mapper.CSysCheckCodeMapper;
import com.lucker.cds.model.CSysCheckCode;
import com.lucker.common.enums.CheckCodeType;
import com.lucker.common.util.Constant;
import com.lucker.common.util.RandomUtil;
import com.lucker.ds.mapper.BaseCustomMapper;
import com.lucker.ds.model.MUser;
import com.lucker.ds.model.SysCheckCode;
import com.lucker.framework.constant.UserRoleType;
import com.lucker.framework.exception.BaseServiceException;
import com.lucker.service.BaseServiceImpl;
import com.lucker.service.model.Sms;
import com.lucker.service.sms.SmsService;
import com.lucker.service.user.MuserService;

@Service
public class SysCheckCodeServiceImpl extends BaseServiceImpl<SysCheckCode, CSysCheckCode, Long> implements SysCheckCodeService {

	
	@Autowired
	SmsService smsService;
	
	@Autowired
	MuserService muserService;
	
	@Override
	public BaseCustomMapper<CSysCheckCode, Long> getCustomMapper() {
		return null;
	}
	
	
	
	@Transactional
	public void sendCheckCode(String mobile,CheckCodeType checkCodeType) {

		
		MUser u = new MUser();
		u.setUserName(mobile);
		List<MUser> ulist = muserService.queryAll(u);
		if(CollectionUtils.isEmpty(ulist)) {
			//return new ResponseEntity<>(new ResponseBaseModel(29001,"该手机号还未注册"), HttpStatus.OK);
			u.setNickName(mobile);
			u.setPassword(UUID.randomUUID().toString());
			u.setIsDelete(false);
			u.setRealName(mobile);
			u.setRoleId(UserRoleType.DEFUALT_ROLE);
			muserService.add(u, false);
		}		
		//检查是否存在有效的验证码
		SysCheckCode querySysCheckCode = new SysCheckCode();
		querySysCheckCode.setMobile(mobile);
		querySysCheckCode.setStatus(Constant.DATA_STATUS_EFFECTIVE);
		querySysCheckCode.setType(checkCodeType.getCode());
		querySysCheckCode.orderByCreateTime(false);
		List<SysCheckCode>  _list = this.queryAll(querySysCheckCode);
		
		//这个地方应该加上短信条数限制，防止恶意短信大批量发送
		CSysCheckCodeMapper customSysCheckCodeMapper = (CSysCheckCodeMapper)this.getBaseCustomMapper();
		Map<String,Object> _map = new HashMap<String,Object>();
		_map.put("type", checkCodeType.getCode());
		_map.put("mobile", mobile);
		_map.put("createTime", DateUtils.addMinutes(new Date(), -10));
		List<CSysCheckCode> customList = customSysCheckCodeMapper.selectCheckCodeByCreateTime(_map);
		if(customList != null && customList.size() > 5) {
			throw new BaseServiceException("每个手机号在10分钟之内最多请求5次注册短信验证码");
		}
		
		
		//将之前发的注册短信验证码作废
		for(SysCheckCode _checkcode : _list) {
			_checkcode.setStatus(Constant.DATA_STATUS_INVALID);
			this.modify(_checkcode, false);
		}
		
		
		String _code = RandomUtil.getRandomNumber(6);
		SysCheckCode checkCode = new SysCheckCode();
		checkCode.setCode(_code);
		checkCode.setCreateTime(new Date());
		checkCode.setMobile(mobile);
		checkCode.setUpdateTime(DateUtils.addMinutes(new Date(), 10));
		checkCode.setType(checkCodeType.getCode());
		checkCode.setStatus(Constant.DATA_STATUS_EFFECTIVE);
		this.add(checkCode, false);
		
		//
		Sms sms = new Sms();
		String msgContent = "你的注册验证码为:" + checkCode.getCode() + " 有效时间10分钟 , 请勿将验证码泄露于他人!";
		sms.setMobile(checkCode.getMobile());
		sms.setSmsTextContant(msgContent);
		sms.setPostfix("【小铺来了】");
		sms.setAsyn(false);
		smsService.sendSms(sms);		
	}
	
	
	public boolean checkCode(String mobile,String code,CheckCodeType checkCodeType) {
		
		//检查是否存在有效的验证码
		SysCheckCode querySysCheckCode = new SysCheckCode();
		querySysCheckCode.setMobile(mobile);
		querySysCheckCode.setStatus(Constant.DATA_STATUS_EFFECTIVE);
		querySysCheckCode.setType(checkCodeType.getCode());
		querySysCheckCode.setCode(code);
		querySysCheckCode.orderByCreateTime(false);
		List<SysCheckCode>  _list = this.queryAll(querySysCheckCode);
		if(CollectionUtils.isEmpty(_list)) {
			throw new BaseServiceException("无效的验证码,请重新输入");
		}
		
		if(_list.size() > 1) {
			throw new BaseServiceException("存在两条同样类型且有效的验证码，请重新发送一条验证码");
		}
		
		if((new Date().getTime() -_list.get(0).getCreateTime().getTime()) > 10 * 60 * 1000) {
			throw new BaseServiceException("验证码有效时间为10分钟, 您输入的验证码已过期,请重新发送!");
		}
		
		//将验证码作废了
		SysCheckCode _c = _list.get(0);
		_c.setStatus(Constant.DATA_STATUS_INVALID);
		modify(_c, false);
		
		return Boolean.TRUE;
		
	}
}
